from apps.merchant.activity.dao import ActivityDao
import json
from decimal import Decimal

def countProfitFun(params):
    activity = ActivityDao().findOneByObjId(params.get('activityId'))
    sellingPrice = Decimal(params.get('sellingPrice'))
    costPrice = Decimal(params.get('costPrice'))

    activityParamsObj = json.loads(activity.activityParams)
    if params.get('scene') == 'giftSection':
        # 设置购买者锁仓比例为3倍
        activityParamsObj['selfSendIncomesLockedRate'] = 3
    # 如果是礼包商品且金额大于等于30000，也送3倍锁仓
    if params.get('scene') == 'generay' and activity.key == 'giftpacks' and sellingPrice * Decimal(params.get('payCount')) >= 30000:
        activityParamsObj['selfSendIncomesLockedRate'] = 3
    if activity.key == 'generalGoods' or activity.key == 'giftpacks':
        # 赠送购买者锁仓
        selfSendIncomesLocked = sellingPrice* Decimal(activityParamsObj.get('selfSendIncomesLockedRate'))
        # 直推赠送购物积分
        verticalSendIncomes = sellingPrice * Decimal(activityParamsObj.get('verticalSendIncomesRate'))
        # 日分红
        dayAward = sellingPrice * Decimal(activityParamsObj.get('dayAwardRate'))
        # 运营补贴
        operationAward = sellingPrice * Decimal(activityParamsObj.get('operationAwardRate'))
        # 净值
        netValue = sellingPrice - (costPrice + verticalSendIncomes + dayAward + operationAward)
        # 月分红
        monthAward = netValue * Decimal(activityParamsObj.get('monthAwardRate'))
        # 供应链奖励
        supplyAward = sellingPrice * Decimal(activityParamsObj.get('supplyAwardRate'))
        # 助残基金
        helpFund = sellingPrice * Decimal(activityParamsObj.get('helpFundRate'))
        # 毛利润
        grossProfit = sellingPrice - (costPrice + verticalSendIncomes + operationAward + dayAward + monthAward + helpFund)
        profitObj = {
            'selfSendIncomesLocked': str(selfSendIncomesLocked.quantize(Decimal('0.00'))),
            'verticalSendIncomes': str(verticalSendIncomes.quantize(Decimal('0.00'))),
            'dayAward': str(dayAward.quantize(Decimal('0.00'))),
            'operationAward': str(operationAward.quantize(Decimal('0.00'))),
            'monthAward': str(monthAward.quantize(Decimal('0.00'))),
            'supplyAward': str(supplyAward.quantize(Decimal('0.00'))),
            'helpFund': str(helpFund.quantize(Decimal('0.00'))),
            'grossProfit': str(grossProfit.quantize(Decimal('0.00'))),
        }
        return profitObj
    elif activity.key == 'discountGoods':
        # 赠送购买者锁仓
        selfSendIncomesLocked = sellingPrice* Decimal(activityParamsObj.get('selfSendIncomesLockedRate'))
        # 净值
        netValue = sellingPrice - costPrice
        # 直推赠送购物积分
        verticalSendIncomes = netValue * Decimal(activityParamsObj.get('verticalSendIncomesRate'))
        # 日分红
        dayAward = netValue * Decimal(activityParamsObj.get('dayAwardRate'))
        # 运营补贴
        operationAward = netValue * Decimal(activityParamsObj.get('operationAwardRate'))
        # 月分红
        monthAward = netValue * Decimal(activityParamsObj.get('monthAwardRate'))
        # 供应链奖励
        supplyAward = netValue * Decimal(activityParamsObj.get('supplyAwardRate'))
        # 助残基金
        helpFund = netValue * Decimal(activityParamsObj.get('helpFundRate'))
        # 毛利润
        grossProfit = sellingPrice - (costPrice + verticalSendIncomes + operationAward + dayAward + monthAward + helpFund)
        profitObj = {
            'selfSendIncomesLocked': str(selfSendIncomesLocked.quantize(Decimal('0.00'))),
            'verticalSendIncomes': str(verticalSendIncomes.quantize(Decimal('0.00'))),
            'dayAward': str(dayAward.quantize(Decimal('0.00'))),
            'operationAward': str(operationAward.quantize(Decimal('0.00'))),
            'monthAward': str(monthAward.quantize(Decimal('0.00'))),
            'supplyAward': str(supplyAward.quantize(Decimal('0.00'))),
            'helpFund': str(helpFund.quantize(Decimal('0.00'))),
            'grossProfit': str(grossProfit.quantize(Decimal('0.00'))),
        }
        return profitObj